package com.hongsuan.pplive.mobileAPI.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tls.sigcheck.tls_sigcheck;

import me.chanjar.weixin.common.util.StringUtils;

public class SignatureUtils {

	private static final Logger logger = LoggerFactory.getLogger(SignatureUtils.class);
	private static final String JNICHEK = "lib/jni/jnisigcheck.so";
	private static final String PRIVATEKEY = "private_key";
//	private static final String SDKAPPID = "1400008942";
//	private static final String EXPIRETIME = "31536000";// one year.

	public static String getSig(final String userId, final String sdkAppId, final String expireTime) throws Exception {
		if (StringUtils.isBlank(userId)) {
			throw new IOException("The userid is not allow empty or null!");
		}
		String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		File file = new File(basePath);
		String parent = file.getParent();
		String jniPath = parent + File.separator + JNICHEK;
		System.out.println("----------->" + jniPath);
		file = new File(jniPath);
		if (!file.exists()) {
			throw new IOException("Your jnisigcheck.so file is not exited!");
		}
		String eckeyPemFile = basePath + File.separator + PRIVATEKEY;
		File priKeyFile = new File(eckeyPemFile);
		if (!priKeyFile.exists()) {
			throw new IOException("Your private key is not exited!");
		}

		tls_sigcheck sig = new tls_sigcheck();
		sig.loadJniLib(jniPath);
		StringBuilder strBuilder = new StringBuilder();
		String s = "";
		BufferedReader br = new BufferedReader(new FileReader(priKeyFile));
		while ((s = br.readLine()) != null) {
			strBuilder.append(s + '\n');
		}
		br.close();
		String priKey = strBuilder.toString();
		int ret = sig.tls_gen_signature_ex2_with_expire(sdkAppId, userId, priKey, expireTime);
		if (0 != ret) {
			logger.warn("ret " + ret + " " + sig.getErrMsg());
			return null;
		} else {
			logger.info("sig:\n" + sig.getSig());
			return sig.getSig();
		}
	}

//	public static String getSig(String userId) throws Exception {
//	
//		return getSig(userId, SDKAPPID, EXPIRETIME);
//	}

	public static void main(String args[]) throws Exception {
		/*String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		File file = new File(basePath);
		String parent = file.getParent();
		System.out.println(basePath);
		System.out.println(parent);*/
		// System.out.println(SignatureUtils.class.getResource(""));
		// System.out.println(SignatureUtils.class.getResource("/"));
		// System.out.println(new File("/").getAbsolutePath());
		// System.out.println(Servlets.getRequest().getContextPath());
		// 使用前请修改动态库的加载路径
		// demo.loadJniLib("D:\\src\\oicq64\\tinyid\\tls_sig_api\\windows\\64\\lib\\jni\\jnisigcheck.dll");
		/// home/cgli/workspace/pplive/src/main/webapp/WEB-INF/lib/jni/jnisigcheck.so

		// tls_sigcheck sig = new tls_sigcheck();
		// String path = new File(JNICHEK).getCanonicalPath();
		// sig.loadJniLib(path);
		//
		// String jni =
		// "/home/cgli/workspace/pplive/src/main/webapp/WEB-INF/lib/jni/jnisigcheck.so";
		// String eckeyPemFile =
		// "/home/cgli/workspace/pplive/src/main/resources/private_key";

		// File priKeyFile = new File(eckeyPemFile);
		// StringBuilder strBuilder = new StringBuilder();
		// String s = "";
		//
		// BufferedReader br = new BufferedReader(new FileReader(priKeyFile));
		// while ((s = br.readLine()) != null) {
		// strBuilder.append(s + '\n');
		// }
		// br.close();
		// String priKey = strBuilder.toString();
		// //int ret = sig.tls_gen_signature_ex2("1400008942", "xiaojun",
		// priKey,"");
		// int ret = sig.tls_gen_signature_ex2_with_expire("1400008942",
		// "xiaojun", priKey,"7776000");
		// if (0 != ret) {
		// System.out.println("ret " + ret + " " + sig.getErrMsg());
		// }
		// else
		// {
		// System.out.println("sig:\n" + sig.getSig());
		// }

		// File pubKeyFile = new File("public.pem");
		// br = new BufferedReader(new FileReader(pubKeyFile));
		// strBuilder.setLength(0);
		// while ((s = br.readLine()) != null) {
		// strBuilder.append(s + '\n');
		// }
		// br.close();
		// String pubKey = strBuilder.toString();
		// ret = sig.tls_check_signature_ex2(sig.getSig(), pubKey, "1400000267",
		// "xiaojun");
		// if (0 != ret) {
		// System.out.println("ret " + ret + " " + sig.getErrMsg());
		// }
		// else
		// {
		// System.out.println("--\nverify ok -- expire time " +
		// sig.getExpireTime() + " -- init time " + sig.getInitTime());
		// }
		
		String SDKAPPID = "1400008942";
		String userId ="mdbobo";
		String sig  = getSig(userId,SDKAPPID,"31536000");
		System.out.println(sig);
		
		
	}

}
